import re

commands_loc = 'Proj_9.txt'
project_name = "bootimage"
project_file = "bootimage.bdf"
project_loc = "/home/DFMTC/dfrd-wliang/workspace/p20_raite_mtk8675_231020/out_full_p20/target/product/spm8675p1_64_wifi_raite/obj/KERNEL_OBJ"
project_reg = r'.*'

def contains_reg(s):  
    print("reg:" + s)
    match = re.search(project_reg, s)  
    return match is not None 


def contains_args(args):
    if not contains_reg(args[0]):
        return False
    for para in args:
        if (para.endswith(".c") \
            or para.endswith(".cc") \
            or para.endswith(".cpp") \
            or para.endswith(".cxx")) \
            and para.find("touchscreen") != -1:
            return True
    return False

with open(commands_loc, 'r') as commands_file:  
    with open(project_file, 'w') as output_file:  
        for line in commands_file:  
            line = line.strip()  
            if len(line)>0:
                args = line.split()                
                if args:  
                    if contains_args(args):
                        output_file.write("------- cpptestscan v.10.6.3.20231201B3901 (cpptesttrace) -------\r\n")
                        output_file.write("working_dir=")
                        output_file.write(project_loc)
                        output_file.write("\r\n")
                        output_file.write("project_name=")
                        output_file.write(project_name)
                        output_file.write("\r\n")
                        for arg in args:  
                            output_file.write("arg=" + arg + '\r\n')  

